package com.peace.hospital.feign.outpatientdepartment.controller;

import com.alibaba.fastjson.JSON;
import com.peace.hospital.entity.common.Department;
import com.peace.hospital.entity.common.People;
import com.peace.hospital.entity.common.ResponseBean;
import com.peace.hospital.entity.outpatientdept.Chargingitems;
import com.peace.hospital.entity.outpatientdept.Patientinformation;
import com.peace.hospital.entity.outpatientdept.dto.RegistrationEntryDto;
import com.peace.hospital.feign.outpatientdepartment.service.OutpatientDepatmentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
/**
 * 门诊模块*/
@RestController
@RequestMapping
public class OutpatentDepatmentController {
    @GetMapping("/logout")
    // @RequiresAuthentication
    //@RequiresRoles(value = {"root", "system"}, logical = Logical.OR)
    public String userLogout() {
        Subject subject = SecurityUtils.getSubject();//获取当前用户
        subject.logout();
        return JSON.toJSONString(new ResponseBean(200,"success"));
    }

    // 绑定变量名字和属性，参数封装进类
    @InitBinder("people")
    public void initBinderPeople(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("student.");
    }
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("chargingitems")
    public void initBinderChargingitems(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("chargingitems.");
    }
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("patientinformation")
    public void initBinderPatientinformation(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("patientinformation.");
    }
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("department")
    public void initBinderDepartment(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("department.");
    }

    @Autowired
    private OutpatientDepatmentService outpatientDepatmentService;

/**添加挂号信息*/
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @RequiresRoles(value = {"root", "outpatentdepatment"}, logical = Logical.OR)
    public String addPeopleRegister(@ModelAttribute("people") People people,@ModelAttribute("chargingitems") Chargingitems chargingitems,@ModelAttribute("patientinformation") Patientinformation patientinformation,@ModelAttribute("department") Department department) {
        RegistrationEntryDto registrationEntryDto = new RegistrationEntryDto(people,chargingitems,patientinformation,department);
        return outpatientDepatmentService.addPeopleRegister(registrationEntryDto);
    }

    /**退号操作*/
    @RequestMapping(value = "/delete",method = RequestMethod.PUT)
    @RequiresRoles(value = {"root", "outpatentdepatment"}, logical = Logical.OR)
    public String updatePeopleRegisterByid(Integer patid) {
        return outpatientDepatmentService.updatePeopleRegisterByid(patid);
    }

    /**
     * 通过日期查询挂号信息*/
    @RequestMapping(value = "/select",method = RequestMethod.GET)
    @RequiresRoles(value = {"root", "outpatentdepatment"}, logical = Logical.OR)
    public String findPeopleRegisterByDate(Patientinformation patientinformation) {
        return outpatientDepatmentService.findPeopleRegisterByDate(patientinformation);
    }
}
